package com.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.Callable;

/**
 * Description:
 *
 * @author lijie
 * @create 2019-01-20 19:31
 * Update by lijie 2019-01-20 19:31
 */
@Controller
public class UserController {

    @RequestMapping("/async01")
    @ResponseBody
    public Callable<String> async01(){
        System.out.println("主线程开始:" + Thread.currentThread() + "==>" + System.currentTimeMillis());
        Callable<String> callable = new Callable<String>() {

            @Override
            public String call() throws Exception {
                System.out.println("副线程开始:" + Thread.currentThread() + "==>" + System.currentTimeMillis());
                Thread.sleep(2000);
                System.out.println("副线程结束:" + Thread.currentThread() + "==>" + System.currentTimeMillis());
                return "Callable<String> async01()";
            }
        };
        System.out.println("主线程结束:" + Thread.currentThread() + "==>" + System.currentTimeMillis());
        return callable;
    }
    @RequestMapping("/async02")
    @ResponseBody
    public DeferredResult<String> async02(){
        System.out.println("主线程开始:" + Thread.currentThread() + "==>" + System.currentTimeMillis());
        DeferredResult<String> deferredResult = new DeferredResult<>();
        System.out.println("主线程结束:" + Thread.currentThread() + "==>" + System.currentTimeMillis());
        return deferredResult;
    }

}
